package com.example.web.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import com.example.web.config.RedisUtil;
import com.example.web.entity.Girl;
@CrossOrigin(origins ="http://localhost",maxAge=3600)
@RestController
public class GirlController {

	
	@Autowired
	RedisUtil redisUtil;
	/**
	 * String
	 * @return
	 */
	@GetMapping("/test1")
	public Girl test() {
		Girl girl = new Girl(1,"a","b","c");
		redisUtil.set("1", girl,60);
		return (Girl)redisUtil.get("1");
	}
	/**
	 * 	自增
	 * @return
	 */
	@GetMapping("/test2")
	public Map<String, Object> test1() {
		Map<String,Object> map = new HashMap<String, Object>();
		//实现原子性的递增，可应用于高并发的秒杀活动、分布式序列号生成等场景。这里我使用它来计数实现一分钟内只接受一次请求。
		String key = "SMS_LIMIT1832236039";
		Long count = redisUtil.incr(key, 1);
		if(count == 1) {
			redisUtil.expire(key, 60);
			map.put("code", 200);
			map.put(key, "发送成功！");
		}
		if(count > 1) {
			map.put("code", -1);
			map.put("msg", "短信发送频繁！");
		}
		return map;
	}
	
	/**
	 * 
	 * @return
	 */
	@GetMapping("/test3")
	public Map<String, Object> test2() {
		Map<String, Object> map1 = new HashMap<String, Object>();
		map1.put("name", "rose");
		map1.put("cup", "36D");
		String key = "PAPAPASQ_rose123";
		redisUtil.hmset(key, map1);
		System.out.println(redisUtil.hmget(key));
		Map<String, Object> map2 = new HashMap<String, Object>();
		String key2 = "PAPAPASQ_jack123";
		map2.put("name", "jack");
		map2.put("dirAdress", "TATAN");
		redisUtil.hmset(key2, map2, 60);
		System.out.println(redisUtil.hmget(key2));
		Map<String,Object> map = new HashMap<String, Object>();
		map.put("rose",map1);
		map.put("jack",map2);
		return  map;
	}
	
	@GetMapping("/test4")
	public String test3() {
		String key = "PAPAPASQ_rose123";
		redisUtil.hset(key, "age", "18", 60);
		return (String)redisUtil.hget(key, "age");
	}
	
	@GetMapping("/test5")
	public Map<String,Object> test4() {
		String key ="BEAUTYFULGIRL_ROSE";
		redisUtil.sSet(key, "rose","18","36D");
		Map<String,Object> map = new HashMap<String, Object>();
		map.put("rose", redisUtil.sGet(key));
		return map;
	}
	
	@GetMapping("/test6")
	public List<Object> test5() {
		String key = "BEAUTYFULGIRL_MARAY";
		List<Object> list = new ArrayList<Object>();
		list.add("maray");
		list.add("18");
		list.add("36D");
		redisUtil.lSet(key, list);
		System.out.println(redisUtil.lGetListSize(key));
		redisUtil.lSet(key, "isBeautyful");
		System.out.println(redisUtil.lGetListSize(key));
		System.out.println(redisUtil.lGetIndex(key, 3));
		System.out.println(redisUtil.lGet(key, 0, 10));
		return redisUtil.lGet(key, 0, 10);
	}
	
	@GetMapping("/test7")
	public long test6() {
		String key = "BEAUTYFULGIRL_MARAY";
		return redisUtil.lRemove(key, 100, "18");
	}
}
